<?php
namespace App\Service;

class HwssService extends BaseService
{
    //货物搜索

    //关键词
    public static function hwByKey($hw_kw)
    {
        $res = self::dbConn(self::$dbHuowu)
            ->select('id', 'title', 'hw_code')
            ->where('del', 0)
            ->where('hw_code', 'like', '%'.$hw_kw.'%')
            ->get();
        if (!count($res)) {
            $res = self::dbConn(self::$dbHuowu)
                ->select('id', 'title', 'hw_code')
                ->where('del', 0)
                ->where('title', 'like', '%'.$hw_kw.'%')
                ->get();
        }
        if (!count($res)) {
            return self::toback(-2, '没有货物');
        }
        $arys = [];
        foreach ($res as $v) {
            $arys[] = [
                'id' => $v->id, 
                'title' => $v->title.'-'.$v->hw_code,
            ];
        }
        return self::toback(0, '', $arys);
    }

    // 搜仓位
    public static function cwList($hw_id, $num)
    {
        $res = self::dbConn(self::$dbHwcw)
            ->where('del', 0)
            ->where('hw_id', $hw_id)
            ->orderBy('stock', 'asc')
            ->skip(0)->take(10)
            ->get();
        if (!count($res)) {
            return self::toback(-2, '没有空位');
        }
        $arys = [];
        foreach ($res as $v) {
            $cwOne = (array) self::dbConn(self::$dbCangwei)
                ->where('id', $v->cw_id)
                ->first();
            $title = '';
            if (!empty($cwOne)) {
                $title = $cwOne['title'];
                $shelf_id = $cwOne['shelf_id'];
            }
            if (!empty($cwOne) && $cwOne['pid']) {
                $cwPidOne = (array) self::dbConn(self::$dbCangwei)
                    ->where('id', $cwOne['pid'])
                    ->first();
                if (!empty($cwPidOne) && $cwPidOne['title']) {
                    $title = $cwPidOne['title'].'-'.$title;
                }
                if (!empty($cwPidOne)) {
                    $shelf_id = $cwPidOne['shelf_id'];
                }
            }
            $shelfOne = (array) self::dbConn(self::$dbShelf)
                ->where('id', $shelf_id)
                ->first();
            if (!empty($shelfOne) && $shelfOne['title']) {
                $title = $shelfOne['title'].'-'.$title;
            }
            $hwOne = (array) self::dbConn(self::$dbHuowu)
                ->where('id', $v->hw_id)
                ->first();
            $hw_name = empty($hwOne) ? '-' : $hwOne['title'];
            $hw_code = empty($hwOne) ? '-' : $hwOne['hw_code'];
            $hw_num = $v->total < $v->stock ? 0 : $v->total - $v->stock;
            $arys['data'][] = [
                'id' => $v->hw_id, 'cw_name' => $title,
                'hw_name' => $hw_name, 'hw_code' => $hw_code, 
                'stock' => $v->stock, 'hw_num' => $hw_num,
            ];
            $arys['shelf'][] = $shelf_id;
        }
        return self::toback(0, '', $arys);
    }

    // 搜货物
    public static function hwList($hw_id, $num)
    {
        $res = self::dbConn(self::$dbHwcw)
            ->where('del', 0)
            ->where('hw_id', $hw_id)
            ->where('stock', '>=', $num)
            ->orderBy('stock', 'desc')
            ->skip(0)->take(10)
            ->get();
        if (!count($res)) {
            return self::toback(-2, '没有货物');
        }
        $arys = [];
        foreach ($res as $v) {
            $cwOne = (array) self::dbConn(self::$dbCangwei)
                ->where('id', $v->cw_id)
                ->first();
            $title = '';
            if (!empty($cwOne)) {
                $title = $cwOne['title'];
                $shelf_id = $cwOne['shelf_id'];
            }
            if (!empty($cwOne) && $cwOne['pid']) {
                $cwPidOne = (array) self::dbConn(self::$dbCangwei)
                    ->where('id', $cwOne['pid'])
                    ->first();
                if (!empty($cwPidOne) && $cwPidOne['title']) {
                    $title = $cwPidOne['title'].'-'.$title;
                }
                if (!empty($cwPidOne)) {
                    $shelf_id = $cwPidOne['shelf_id'];
                }
            }
            $shelfOne = (array) self::dbConn(self::$dbShelf)
                ->where('id', $shelf_id)
                ->first();
            if (!empty($shelfOne) && $shelfOne['title']) {
                $title = $shelfOne['title'].'-'.$title;
            }
            $hwOne = (array) self::dbConn(self::$dbHuowu)
                ->where('id', $v->hw_id)
                ->first();
            $hw_name = empty($hwOne) ? '-' : $hwOne['title'];
            $hw_code = empty($hwOne) ? '-' : $hwOne['hw_code'];
            $arys['data'][] = [
                'id' => $v->hw_id, 'cw_name' => $title,
                'hw_name' => $hw_name, 'hw_code' => $hw_code, 
                'stock' => $v->stock,
            ];
            $arys['shelf'][] = $shelf_id;
        }
        return self::toback(0, '', $arys);
    }
}